package dhm.com.dhmshop.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.SystemClock;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import dhm.com.dhmshop.R;
import dhm.com.dhmshop.base.netWork.Constant;
import dhm.com.dhmshop.entity.GetMyShopinfo;
import dhm.com.dhmshop.entity.MyShops;
import dhm.com.dhmshop.framework.module.commodity.activity.CommodityDetailsActivity;
import dhm.com.dhmshop.updata.activity.ShopdetailActivity;
import dhm.com.dhmshop.utils.CustomJiaJian;

/**
 * Created by admin
 * 2019/4/29
 * 所有订单适配器
 */
public class MyShopsAdapter extends RecyclerView.Adapter<MyShopsAdapter.ViewHolder> {

    private List<MyShops.DataBean> shopList;
    private Context context;


    public MyShopsAdapter(Context context) {
        this.shopList = new ArrayList<>();
        this.context = context;
    }


    public void setShopList(List<MyShops.DataBean> shopList) {
        this.shopList = shopList;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        ViewHolder viewHolder;
        View view= LayoutInflater.from(context).inflate(R.layout.recy_shop, viewGroup,false);
        viewHolder=new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, final int i) {
        viewHolder.shopname.setText(shopList.get(i).getShopName());
        viewHolder.check.setChecked(shopList.get(i).isChecked());
        LinearLayoutManager manager=new LinearLayoutManager(context);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        viewHolder.recy_image.setLayoutManager(manager);
        MyShopsChildAdapter myShopsChildAdapter=new MyShopsChildAdapter(context);
        viewHolder.recy_image.setAdapter(myShopsChildAdapter);
        myShopsChildAdapter.setShopList(shopList.get(i).getList());


        viewHolder.check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if(compoundButton.isPressed()) {
                    for (int j=0;j<shopList.get(i).getList().size();j++){
                        shopList.get(i).getList().get(j).setChecked(viewHolder.check.isChecked());
                    }
                    myShopsChildAdapter.setShopList(shopList.get(i).getList());
                    myShopsChildAdapter.notifyDataSetChanged();
                    shopList.get(i).setChecked(viewHolder.check.isChecked());
                    onshopitemClick.item(i,shopList.get(i));
                }
            }
        });
        viewHolder.shops.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(context, ShopdetailActivity.class);
                intent.putExtra("shop_id",shopList.get(i).getList().get(0).getShopId()+"");
                context.startActivity(intent);
            }
        });

        myShopsChildAdapter.setdeleteClick(new MyShopsChildAdapter.OnClick() {
            @Override
            public void item(int posttion, boolean checked) {
                shopList.get(i).getList().get(posttion).setChecked(checked);
                boolean ischeked=true;
                for (int j=0;j<shopList.get(i).getList().size();j++){
                    if (!shopList.get(i).getList().get(j).isChecked()){
                        ischeked=false;
                        j=shopList.get(i).getList().size();
                    }
                }
                shopList.get(i).setChecked(ischeked);
                viewHolder.check.setChecked(ischeked);
                onshopitemClick.item(i,shopList.get(i));
            }
        });

        myShopsChildAdapter.setaddClick(new MyShopsChildAdapter.OnIntentClick() {
            @Override
            public void item(int posttion, int num) {
                shopList.get(i).getList().get(posttion).setCartNum(num);
                notifyDataSetChanged();
                onshopitemchange.item(i,posttion,shopList.get(i));
            }
        });

        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(context, CommodityDetailsActivity.class);
                intent.putExtra("gid",shopList.get(i).getList().get(0).getGoodsId());
                intent.putExtra("shop_id",shopList.get(i).getShopId());
                context.startActivity(intent);
            }
        });

        viewHolder.shop_num.setText(shopList.get(i).getList().size()+"");

        float shopPrice=0;
        for (int j=0;j<shopList.get(i).getList().size();j++){
            shopPrice=shopPrice+(shopList.get(i).getList().get(j).getShopPrice()*shopList.get(i).getList().get(j).getCartNum());
        }

        DecimalFormat decimalFormat=new DecimalFormat("0.00");
        String resultString=decimalFormat.format(shopPrice);
        viewHolder.shop_price.setText("￥ "+resultString);


//        viewHolder.check.onTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(),SystemClock.uptimeMillis(),MotionEvent.ACTION_UP,0,0,0));

//        viewHolder.num.setCustomListener(new CustomJiaJian.CustomListener() {
//            @Override
//            public void jiajian(int count) {
//                onIntentClick.item(i,count);
//            }
//
//            @Override
//            public void shuRuZhi(int count) {
//
//            }
//        });


    }

    @Override
    public int getItemCount() {
        return shopList.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        public LinearLayout shops;
        public CheckBox shopname;
        public CheckBox check;
        public RecyclerView recy_image;
        public TextView shop_num;
        public TextView shop_price;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            shops=itemView.findViewById(R.id.shops);
            check=itemView.findViewById(R.id.check);
            shopname=itemView.findViewById(R.id.shopname);
            recy_image=itemView.findViewById(R.id.recy_image);
            shop_num=itemView.findViewById(R.id.shop_num);
            shop_price=itemView.findViewById(R.id.shop_price);
        }
    }

    private OnClick onClick;
    public void setdeleteClick(OnClick onClick) {
        this.onClick = onClick;
    }
    public interface OnClick{
        void item(int posttion,boolean checked);
    }

    private OnIntentClick onIntentClick;
    public void setaddClick(OnIntentClick onClick) {
        this.onIntentClick = onClick;
    }
    public interface OnIntentClick{
        void item(int posttion,int num);
    }

    private OnShopClick onshopClick;
    public void setaddClick(OnShopClick onClick) {
        this.onshopClick= onClick;
    }
    public interface OnShopClick{
        void item(int posttion);
    }

    private OnShopItemClick onshopitemClick;
    public void settiemClick(OnShopItemClick onClick) {
        this.onshopitemClick= onClick;
    }
    public interface OnShopItemClick{
        void item(int posttion,MyShops.DataBean databean);
    }


    private OnShopItemChange onshopitemchange;
    public void settiemchange(OnShopItemChange onClick) {
        this.onshopitemchange= onClick;
    }
    public interface OnShopItemChange{
        void item(int i,int posttion,MyShops.DataBean databean);
    }
}

